package yxy.game.pm2.active.module;

import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.bean.actrecharge;
import yxy.game.pm2.active.ActiveUtil;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

import java.util.ArrayList;
import java.util.Map;

public class ActiveModule_累充天数 extends PlayerModule {

	//bean ---------------------------------------------------
	
	private static final int acttype = 9;
	
	private int days;	//累冲天数
	private DateTime updatetime;	//累充更新时间
	private DateTime drawtime;		//领取时间
	private ArrayList<Integer> getids = new ArrayList<Integer>();	//已经领取的奖励列表
	
	public int getDays() {
		return days;
	}
	public void setDays(int days) {
		this.days = days;
	}
	public DateTime getUpdatetime() {
		return updatetime;
	}
	public void setUpdatetime(DateTime updatetime) {
		this.updatetime = updatetime;
	}
	public DateTime getDrawtime() {
		return drawtime;
	}
	public void setDrawtime(DateTime drawtime) {
		this.drawtime = drawtime;
	}
	public ArrayList<Integer> getGetids() {
		return getids;
	}
	public void setGetids(ArrayList<Integer> getids) {
		this.getids = getids;
	}

	//func -----------------------------------------------------------
	
	/**
	 * @param pcode
	 */
	public static ActiveModule_累充天数 create(String pcode) {
		ActiveModule_累充天数 am = new ActiveModule_累充天数();
		am.setPlayerCode(pcode);
		am.setDays(0);
		am.setDrawtime(DateTime.now());
		
		am.set();
		return am;
	}

	public void addPayDay(int payamount) {
		DateTime now = DateTime.now();
		
		if(getUpdatetime() != null && ActiveUtil.isSameDay(now, getUpdatetime())) {
			return;
		}
		
		int newdays = getDays() + 1;
		Map<String, actrecharge> armap = ActDataCenter.getAllData(actrecharge.class, getPlayerCode());
		for (actrecharge data : armap.values()) {
			if(getGetids().contains(data.getId())) {
				continue;
			}
			
			if(newdays >= data.getCondition() && payamount >= data.getRechargemin()) {
				setUpdatetime(now);
				setDays(newdays);
				set();
			}
		}
	}
	
//	public static final int canget = 1;
//	public static final int hadget = 2;
//	public static final int cannotget = 3;

	public void update() {
		if(getUpdatetime() == null) {
			return;
		}
		
		Map<String, actrecharge> armap = ActDataCenter.getAllData(actrecharge.class, getPlayerCode());
		//如果已经领完 并且不是领取那天 重置活动
		if(getGetids().size() >= armap.size() && ActiveUtil.days(getDrawtime(), DateTime.now()) >= 1) {
			reset();
		}
	}

	//重置活动
	private void reset() {
		setDays(0);
		setUpdatetime(null);
		setDrawtime(DateTime.now());
		setGetids(new ArrayList<Integer>());
		
		set();
	}
	
	// redis ---------------------------------------------------------

	static private String key(String playerCode) {
		return String.format("PLAYER:%s:ACTIVE:%S", playerCode, acttype);
	}

	public void set() {
		try {
			String key1 = key(getPlayerCode());
			setv(key1, this);
			check();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取 ActiveModule_累充天数
	 * 
	 * @param playerCode
	 * @return
	 */
	static public ActiveModule_累充天数 get(String playerCode, Boolean isNewPlayer) {
		try {
			ActiveModule_累充天数 module = getv(key(playerCode));

			//新用户不需要去查数据库
			if (module == null && !isNewPlayer) {
				module = MySQLCacheDAO.getV(key(playerCode), ActiveModule_累充天数.class);
				if(module != null){module.set();}//同步到redis
			}

			if (module == null) {
				module = create(playerCode);
			}
			if(module == null){return null;}

			module.update();
			module.check();

			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/** 查看是否有可领取奖励 控制红点 */
	public void check() {
		new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.开启);

		Map<String, actrecharge> armap = ActDataCenter.getAllData(actrecharge.class, getPlayerCode());
		for(actrecharge data : armap.values()) {
			if(getGetids().contains(data.getId())) {
				continue;
			}
			
			if(getDays() >= data.getCondition()) {
				new TipHandle().updateTip(getPlayerCode(), TipType.活动_累冲天数奖励, true);
				return;
			}
		}
		
		new TipHandle().updateTip(getPlayerCode(), TipType.活动_累冲天数奖励, false);
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				ActiveModule_累充天数 activeModule_累充天数 = getv(key(playerCode));
				MySQLCacheDAO.setV(key, activeModule_累充天数);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}

}
